package com.mrs.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mrs.common.HttpEnum;
import com.mrs.common.dto.UserDto;
import com.mrs.entity.User;
import com.mrs.exception.BusinessException;
import com.mrs.mapper.UserMapper;
import com.mrs.service.UserService;
import com.mrs.utils.TokenUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * description: UserServiceImpl
 * date: 2022/7/13 22:55
 * author: MR.孙
 */
@Slf4j
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper,User> implements UserService {
    @Override
    public boolean saveUser(User user) {


        // if(user.getId()==null){
        //     return save(user);
        // }else{
        //     return updateById(user);
        // }

        return saveOrUpdate(user);
    }

    @Override
    public UserDto login(UserDto userDto) throws BusinessException {
        User one = getUserInfo(userDto);
        // 设置token
        String token = TokenUtils.genToken(one.getId().toString(), one.getPassword());
        userDto.setToken(token);
        if(one != null){
            BeanUtil.copyProperties(one,userDto,true);

            return userDto;
        }else{
            throw new BusinessException(HttpEnum.Code.OTHER_ERROR,"用户名或密码错误");
        }

    }

    @Override
    public User register(UserDto userDto) throws BusinessException {
        User one = getUserInfo(userDto);
        if (one == null) {
            one = new User();
            BeanUtil.copyProperties(userDto, one, true);
            save(one);  // 把 copy完之后的用户对象存储到数据库
        } else {
            throw new BusinessException(HttpEnum.Code.ARGS_ERROR, "用户已存在");
        }
        return one;
    }


    // @Autowired
    // private UserMapper userMapper;

    // public int save(User user) {
    //     if (user.getId() == null) {  // user没有id，则表示是新增
    //         return userMapper.insert(user);
    //     } else { // 否则为更新
    //         return userMapper.update(user);
    //     }
    // }

    // @Override
    // public List<User> page(Integer currentPage, Integer pageSize, String username,String email,String address) {
    //     List<User> pageInfo=userMapper.page(currentPage,pageSize,username,email,address);
    //     return pageInfo;
    // }

    public User getUserInfo(UserDto userDto) throws BusinessException {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", userDto.getUsername());
        queryWrapper.eq("password", userDto.getPassword());
        User one;
        try {
            one = getOne(queryWrapper);
        } catch (Exception e) {
            throw new BusinessException(HttpEnum.Code.SERVER_ERROR, "系统错误");
        }
        return one;
    }
}
